package cn.nawang.ebeim.api.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.nawang.ebeim.api.domain.StorageFileToDo;
import cn.nawang.ebeim.api.repository.StorageFileToDoRepository;
import cn.nawang.ebeim.api.repository.impl.StorageFileToDoRepositoryImpl;
import cn.nawang.ebeim.api.service.StorageFileToDoService;
import cn.nawang.ebeim.api.utils.Constant;
import cn.nawang.ebeim.api.utils.ConvertUtil;
import cn.nawang.ebeim.api.vo.StorageFileToDoView;

/**
 * @ClassName: StorageFileToDoServiceImpl
 * @Description: TODO
 * @author fanyb
 * @date Aug 26, 2015 2:55:03 PM
 * @version v1.0
 */
@Service("storageFileToDoService")
public class StorageFileToDoServiceImpl implements StorageFileToDoService {

  @Autowired
  private StorageFileToDoRepository storageFileToDoRepository;

  @Autowired
  private StorageFileToDoRepositoryImpl storageFileToDoRepositoryImpl;

  @Override
  public boolean add(StorageFileToDo storageFileToDo) {
    try {
      storageFileToDoRepository.save(storageFileToDo);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }

  @Override
  public List<StorageFileToDoView> findUntreatedList() {
    List<StorageFileToDoView> list = storageFileToDoRepositoryImpl.findAll(Constant.QUERY_COUNT,
        Constant.STORAGE_FILE_TO_DO_STATUS_NO);
    // StringBuffer updateIds=new StringBuffer();
    // if(!list.isEmpty()){
    // for(int i=0;i<list.size();i++){
    // StorageFileToDoView storageFileToDoView=list.get(i);
    // updateIds.append(storageFileToDoView.getId());
    // updateIds.append(",");
    // }
    // String ids=updateIds.toString();
    // String aa=ids.substring(0,ids.lastIndexOf(","));
    // String[] id=aa.split(",");
    // storageFileToDoRepositoryImpl.update(ConvertUtil.toDbString(id));
    // }
    return list;
  }

  @Override
  public boolean delete(String[] ids) {
    try {
      return storageFileToDoRepositoryImpl.delete(ConvertUtil.toDbString(ids));
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }

  @Override
  public boolean update(StorageFileToDo storageFileTodo) {
    try {
      storageFileToDoRepository.save(storageFileTodo);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }

  @Override
  public boolean isExistStorageFileToDo(String storageCloudId, String datasourceId,
      String signature) {
    List<StorageFileToDo> list =
        storageFileToDoRepositoryImpl.findByProperty(storageCloudId, datasourceId, signature);
    if (!list.isEmpty() && list != null) {
      return true;
    }
    return false;
  }

  @Override
  public StorageFileToDo findByDatasourceIdAndSignature(String datasourceId, String signature) {
    StorageFileToDo storageFileToDo =
        storageFileToDoRepositoryImpl.findByDatasourceIdAndSignature(datasourceId, signature);
    return storageFileToDo;
  }

}
